/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.rrx360.common.dao.CommonDao;
import com.rrx360.common.model.BaseEntity;
import com.rrx360.common.pagequery.PageQueryCondition;
import com.rrx360.common.pagequery.PageQueryResult;
import com.rrx360.common.service.BaseEntityService;

public class BaseEntityServiceImpl<T extends BaseEntity> extends BaseBeanServiceImpl<T>
    implements BaseEntityService<T> {

  protected Class entityClass;

  protected CommonDao commonDao;

  public BaseEntityServiceImpl() {
    Type superType = getClass().getGenericSuperclass();
    if (superType instanceof Class) {
      superType = ((Class) superType).getGenericSuperclass();
    }
    if (superType instanceof ParameterizedType) {
      Object o = ((ParameterizedType) superType).getActualTypeArguments()[0];
      if (o instanceof Class) {
        entityClass = (Class<T>) o;
      }
    } else {
      entityClass = null;
    }
  }

  public T get(Serializable id) {
    return (T) commonDao.get(entityClass, id);
  }

  public T load(Serializable id) {
    return (T) commonDao.load(entityClass, id);
  }

  public List<T> findAll() {
    return commonDao.findAll(entityClass);
  }

  public void add(T entity) {
    commonDao.saveEntity(entity);
  }

  public void update(T entity) {
    if (entity.getVersionLock() == null) {
      T dbEntity = this.get(entity.getId());
      BeanUtils.copyProperties(entity, dbEntity,
          new String[] {"versionLock", "createdOn", "updatedOn", "createdBy", "updatedBy"});
      commonDao.updateEntity(dbEntity);
    } else {
      commonDao.updateEntity(entity);
    }
  }

  public void remove(T entity) {
    commonDao.deleteEntity(entity);
  }

  public Class getEntityClass() {
    return entityClass;
  }

  public void setEntityClass(Class entityClass) {
    this.entityClass = entityClass;
  }

  public PageQueryResult findByPage(PageQueryCondition condition) {
    return commonDao.findByPage(condition, entityClass);
  }

  public void delete(Serializable[] ids) {
    for (Serializable id : ids) {
      delete(id);
    }
  }

  public void delete(Serializable id) {
    commonDao.delete(id, entityClass);
  }

  public T getBeanById(Serializable id) {
    return this.get(id);
  }

  public void addBean(T bean) {
    this.add(bean);
  }

  public void updateBean(T bean) {
    this.update(bean);
  }

  public void deleteBean(Serializable[] ids) {
    for (Serializable id : ids) {
      deleteBean(id);
    }
  }

  public void deleteBean(Serializable id) {
    this.delete(id);
  }



}
